package com.punch.punchtable.controller;

import com.punch.punchtable.entity.Attendance;
import com.punch.punchtable.service.AttendanceService;
import com.punch.punchtable.utils.ExcelUtil;
import com.punch.punchtable.vo.PageResponseVO;
import com.punch.punchtable.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {


    @Autowired
    private AttendanceService service;

    @GetMapping("/page")
    @ResponseBody
    public PageResponseVO page(Attendance attendance, Integer page, Integer limit, HttpServletRequest request, Model model,
                               @RequestParam(name = "date", defaultValue = "", required = false) String date) {

        if (!StringUtils.isEmpty(date)) {
            String[] split = date.split("-");
            attendance.setYear(Long.parseLong(split[0]));
            attendance.setMonth(Long.parseLong(split[1]));
            attendance.setDay(Long.parseLong(split[2]));
        }

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("usernum", ExampleMatcher.GenericPropertyMatchers.contains());

        if (StringUtils.isEmpty(attendance.getDept())) {
            attendance.setDept(null);
        }

        if (StringUtils.isEmpty(attendance.getUsername())) {
            attendance.setUsername(null);
        }

        if (StringUtils.isEmpty(attendance.getUsernum())) {
            attendance.setUsernum(null);
        }

        Example<Attendance> of = Example.of(attendance, matcher);
        PageRequest pageRequest = new PageRequest(page - 1, limit, new Sort(Sort.Direction.ASC, "aid"));
        Page<Attendance> byPage = service.findByPage(of, pageRequest);
        return PageResponseVO.response(byPage);

        //获取用户信息


    }

    @GetMapping("/dept")
    @ResponseBody
    public ResponseVO dept() {
        List<String> dept = service.findDept();
        return ResponseVO.ok(dept);
    }


    @GetMapping("/export")
    public void export(Attendance attendance,
                       @RequestParam(name = "date", defaultValue = "", required = false) String date,
                       HttpServletResponse response) {

        if (!StringUtils.isEmpty(date)) {
            String[] split = date.split("-");
            attendance.setYear(Long.parseLong(split[0]));
            attendance.setMonth(Long.parseLong(split[1]));
            attendance.setDay(Long.parseLong(split[2]));
        }

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("usernum", ExampleMatcher.GenericPropertyMatchers.contains());

        if (StringUtils.isEmpty(attendance.getDept())) {
            attendance.setDept(null);
        }


        if (StringUtils.isEmpty(attendance.getUsername())) {
            attendance.setUsername(null);
        }

        if (StringUtils.isEmpty(attendance.getUsernum())) {
            attendance.setUsernum(null);
        }


        Example<Attendance> of = Example.of(attendance, matcher);
        List<Attendance> all = service.findAll(of);

        List<String> titleList = Arrays.asList("编号", "年", "月", "日", "用户名", "工号", "部门", "通道", "最早时间", "最晚时间", "是否迟到", "是否早退", "是否领导");

        List<List<String>> dataList = new ArrayList<>();
        for (Attendance a : all) {
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(a.getAid()));
            list.add(String.valueOf(a.getYear()));
            list.add(String.valueOf(a.getMonth()));
            list.add(String.valueOf(a.getDay()));
            list.add(String.valueOf(a.getUsername()));
            list.add(String.valueOf(a.getUsernum()));
            list.add(String.valueOf(a.getDept()));
            list.add(String.valueOf(a.getChannel()));
            list.add(String.valueOf(a.getMintime()));
            list.add(String.valueOf(a.getMaxtime()));
            list.add(String.valueOf(a.getIslate()));
            list.add(String.valueOf(a.getIsearly()));
            list.add(String.valueOf(a.getRole()));
            dataList.add(list);
        }

        ExcelUtil.uploadExcelAboutUser(response,
                String.format("出勤_%d.xlsx", System.currentTimeMillis()),
                titleList,
                dataList);
    }


}
